package com.woniu.supermarket.web;

import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.Stock;
import com.woniu.supermarket.entity.Warehousing;
import com.woniu.supermarket.result.ResultResponse;
import com.woniu.supermarket.service.IGoodsService;
import com.woniu.supermarket.service.IStockService;
import com.woniu.supermarket.vo.StockVo;
import com.woniu.supermarket.vo.WarehousingVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("stock")
public class StockController {
    @Autowired
    IStockService iStockService;


    @GetMapping("/stlist")
    @ApiOperation(value = "分页库存操作")
    public ResultResponse<PageInfo<StockVo>> stlist(@ApiParam(name="now",value = "当前页")@RequestParam(name = "now",defaultValue = "1") Integer now,
                                                    @ApiParam(name="size",value = "页大小")@RequestParam(name = "size",defaultValue = "3") Integer size,
                                                    String name){
        ResultResponse<PageInfo<StockVo>> result = null;
        try {
            PageInfo<StockVo> pageInfo = iStockService.findStockByCons(now, size, name);
            result = new ResultResponse<>(200,"查询仓库成功",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询仓库失败");
        }
        return result;
    }

    @PutMapping("/modify")
    @ApiOperation(value = "修改库存下限操作")
    public ResultResponse<Void> modifyStockLimit(@RequestBody Stock stock) {
        ResultResponse<Void> result = null;
        try {
            iStockService.modifyStockLimit(stock);
            result = new ResultResponse<>(200, "修改库存下限成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "修改库存下限失败");
        }
        return result;
    }

    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "删除库存操作")
    public ResultResponse<Void> delStock(@PathVariable("id")Integer id) {
        ResultResponse<Void> result = null;
        try {
            int i = iStockService.removeStock(id);
            result = new ResultResponse<>(200, "删除库存成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "删除库存" +
                    "失败");
        }
        return result;
    }




    @GetMapping("/findStock")
    @ApiOperation(value = "查询库存操作")
    public ResultResponse<PageInfo<StockVo>> findStock(@ApiParam(name="now",value = "当前页")@RequestParam(name = "now",defaultValue = "1") Integer now,
                                                    @ApiParam(name="size",value = "页大小")@RequestParam(name = "size",defaultValue = "3") Integer size){
        ResultResponse<PageInfo<StockVo>> result = null;
        try {
            PageInfo<StockVo> pageInfo = iStockService.findStock(now, size);
            result = new ResultResponse<>(200,"查询仓库成功",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询仓库失败");
        }
        return result;
    }

}
